<?php
namespace Home\Controller;
use Think\Controller;
class DetailController extends PublicController {
	
    public function goodsdetail(){
		$this->assign("k",CONTROLLER_NAME);
		$this->assign("f",ACTION_NAME);
		$this->show();
		$this->wxjshow();
		$this->showpinglun();
		$this->display();
	}
	//在goodsdetail详情页面显示输出
	public function show(){
		$this->showpinglun();
		//取到要显示商品的id
		$id = $_GET['uid'];
		//var_dump($id);
		$res = M('listinfo')->where("id = $id")->find();
		//var_dump($res);
		$this->assign("res",$res);
		
	}
	
	//添加商品到购物车
	public function addTocart(){
		$id = $_POST['id'];
		$name = session('name');
		$ids = M('user')->where("name = '$name'")->find();
		$data1['user_id'] = $ids['id']; 
		$data1['p_id']=$_POST["id"];
		$data1['name']=$_POST["name"];
		$data1['number']=$_POST["number"];
		$data1['price']=$_POST["price"];
		//shoping 购物车数据库的数据
		$res = M('shoping')->select();
		foreach($res as $k=>$v){
			//echo $res[$k]['p_id'];
			
			$allid[]=$res[$k]['p_id'];
			
			
		}
		//var_dump($allid);
		//如果有相同id的商品就让number+1
		if(in_array($id, $allid)){        //看$allid数组里是否存在ajax过来的id号
				$res = M('shoping')->where("p_id = '$id' and status = '1'")->find();
				//var_dump($res);
				if(empty($res)){
					
					$info = M('shoping')->add($data1);
					
				}else{
					
					$data['number']=$res['number']+1;
					M('shoping')->where("p_id = '$id'")->save($data);
				}
				
				
			}else{
				$info = M('shoping')->add($data1);
			}
			

		
		//echo $_POST["name"]; 调试是否能取到ajax传过来的值
		
		//ajax success里跳转到别的地址
		
	}
	//添加评论
	public function pinglun(){
		$user_name =  session('name');
		$data['user'] = $user_name;
		if(empty($user_name)){
			$this->redirect("Public/login");
		}else{
			$id = $_POST['id'];
			$data['p_id'] = $id;
			$data['content'] = $_POST["content"];
			$data['title'] = $_POST["titles"];
			$data['create_time'] = date("Y-m-d H:i:s");
			var_dump($data);
			M('comment')->add($data);
			$this->redirect("Index/index/?id=$id");
			//$this->redirect("Index/index");
		}
	}
	
	//输出评论
	public function showpinglun(){
		$id =$_GET['uid'];
		$user_name =  session('name');
		$commentinfo = M('comment')->where("user = '$user_name' and p_id = '$id'")->select();
		$this->assign("commentinfo",$commentinfo);
	}
}